package com.qingcheng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qingcheng.dao.BrandMapper;
import com.qingcheng.entity.PageResult;
import com.qingcheng.entity.Result;
import com.qingcheng.pojo.Brand;
import com.qingcheng.service.goods.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

/**
 * @Author: wy
 * @Date:2019/9/9 23:30
 */
@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;

    @Override
    public List<Brand> findAll() {
        return brandMapper.selectAll();
    }

    @Override
    public PageResult<Brand> findPage(Integer page, Integer size) {
        /*分页插件*/
        PageHelper.startPage(page, size);
        /*直接查询到Page<Brand>*/
        Page<Brand> brands = (Page<Brand>) brandMapper.selectAll();
        /*只要page<brand>当中的两个 参数   Total和 List<Brand>*/
        /*有参构造赋值*/
        PageResult<Brand> pageResult = new PageResult<>();
        pageResult.setRows(brands.getResult());
        pageResult.setTotal(brands.getTotal());
        return pageResult;
    }

    @Override
    public List<Brand> findList(Map<String, Object> map) {
        Example  example = getExample(map);
        List<Brand> brands = brandMapper.selectByExample(example);

        return brands;
    }

    @Override
    public PageResult<Brand> findPageCondition(Map<String, Object> map, Integer page, Integer size) {
             Example example= getExample(map);
                PageHelper.startPage(page,size);
        Page<Brand> brands = (Page<Brand>) brandMapper.selectByExample(example);
                     PageResult<Brand> pageResult=new PageResult<>();
                     pageResult.setTotal(brands.getTotal());
                     pageResult.setRows(brands.getResult());
                     return pageResult;
    }

    @Override
    public Brand findById(Integer id) {
       return brandMapper.selectByPrimaryKey(id);

    }

    @Override
    public int add(Brand brand) {
       return brandMapper.insert(brand);

    }

    @Override
    public int update(Brand brand) {
        /*使用updateByPrimaryKeySelective会忽略brand中的null值，不会写入到数据库*/
        return brandMapper.updateByPrimaryKeySelective(brand);
    }

    @Override
    public int delete(Integer id) {
        return brandMapper.deleteByPrimaryKey(id);
    }

    public Example getExample(Map<String, Object> map) {
        Example e = new Example(Brand.class);
        Example.Criteria criteria = e.createCriteria();
        if (map != null) {

            if (map.get("name") != null && !"".equals(map.get("name"))) {
                criteria.andLike("name", "%" + (String) map.get("name") + "%");
            }
            if (map.get("letter") != null && !"".equals((map.get("letter")))) {
                criteria.andLike("letter", "%" + (String) map.get("letter") + "%");
            }
        }
        return e;
    }
}
